package veilingsite.servlets.nl;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import veilingsite.dao.nl.UserDao;
import veilingsite.dao.nl.VeilingDao;
import veilingsite.database.nl.DBmanager;

/**
 * Servlet implementation class MyContextListener
 */
public class MyContextListener implements javax.servlet.ServletContextListener {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyContextListener() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
    	ServletContext servletContext = event.getServletContext();
	    String dbLocation	=  servletContext.getInitParameter("dbLocation");
	    String dbUrl 		=  servletContext.getInitParameter("dbURL");
	    String dbName 		= servletContext.getInitParameter("dbName");

        DBmanager myDb = DBmanager.getInstance(dbName, dbUrl, dbLocation);
        
        VeilingDao vDao = new VeilingDao(myDb);
        UserDao uDao = new UserDao(myDb);
        
        //create tables if needed
        vDao.createTablesIfNeeded();
        uDao.createTablesIfNeeded();
        
        // prepare statements
        vDao.init();
        uDao.init();
        
       // put dao's in the servlet context so other servlets can acces them
        servletContext.setAttribute("VeilingDao", vDao);
        servletContext.setAttribute("UserDao", uDao);
		
	}

}
